﻿Imports _1065081_1065104.SPDTO
Imports _1065081_1065104.SPBUS
Imports _1065081_1065104.Global_Class
Public Class Thêm_Mới_Căn_Hộ
    Private _nvbus As New cbusisanpham()

#Region " form moving"
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Sub BorderlessWindowDrag(ByVal sender As Object, ByVal e As MouseEventArgs)
        ReleaseCapture()
        SendMessage(Me.Handle, &HA1, 2, 0&)
    End Sub
    Private Sub frmAddsanpham_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        BorderlessWindowDrag(sender, e)
    End Sub

#End Region
#Region "Windows Events"
    Private Sub themsanpham_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        xoa.Enabled = True
        sua.Enabled = True
        luu.Enabled = True
    End Sub
#End Region
#Region "Windows Events"

    Private Sub them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ReNew()
        txtmach.Text = _nvbus.GetsanphamID("AGBC", "mach", "sanpham", 10).Trim()

        txtloaicanho.Focus()

        luu.Enabled = True
        them.Enabled = False
        them.Enabled = False
    End Sub
#End Region

    Private Sub luu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles luu.Click
        Try
            If CheckDataInput() = True Then
                _nvbus.Insert(AddsanphamValue())
                sua.Enabled = True
                xoa.Enabled = True
                luu.Enabled = False
                them.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Loi xu ly !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

   

    Private Sub sua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sua.Click
        Try
            If CheckDataInput() = True Then
                _nvbus.Update(AddsanphamValue())
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Loi xu ly !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xoa.Click
        Try
            If MessageBox.Show("Bạn có thật sự muốn xóa ?", "Xac nhan", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                _nvbus.Delete(txtmach.Text.Trim())
                ReNew()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Loi xu ly !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
        Me.Close()
    End Sub

#Region "Process"
    Private Function AddsanphamValue() As CsanphamDTO
        Dim sanpham As New CsanphamDTO()
        sanpham.mach = txtmach.Text.Trim()
        sanpham.giatien = txtgiatien.Text.Trim()
        sanpham.diachi = txtdiachi.Text.Trim()
        sanpham.loaich = txtloaicanho.Text.Trim()
        sanpham.manvql = txtnhanvienql.Text.Trim()

        Return sanpham
    End Function
    Private Function CheckDataInput() As Boolean
        Dim strErr As String = String.Empty

        If txtmach.Text.Trim() = "" Then
            strErr = "- Mã căn hộ."
        End If
        If txtgiatien.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- giá tiền căn hộ."
        End If
        If txtnhanvienql.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- nhân viên quản lý căn hộ."
        End If
        If txtdiachi.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Địa chỉ căn hộ."
        End If
        If txtloaicanho.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- chủng loại căn hộ."
        End If


        If strErr <> String.Empty Then
            MessageBox.Show("Bạn phải hoàn tất các thông tin sau :" & vbLf & strErr, "Canh bao !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Return False
        End If

        Return True
    End Function
    Private Sub ReNew()
        txtdiachi.Text = ""
        txtgiatien.Text = ""

        txtloaicanho.Text = ""
        txtmach.Text = ""
        txtnhanvienql.Text = ""


        them.Enabled = True
        luu.Enabled = True
        sua.Enabled = True
        xoa.Enabled = True
    End Sub

#End Region

    Private Sub lam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lam.Click
        ReNew()
    End Sub

    Private Sub them_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles them.Click
        ReNew()
        txtmach.Text = _nvbus.GetsanphamID("AGBC", "mach", "sanpham", 10).Trim()

        txtloaicanho.Focus()

        luu.Enabled = True
        them.Enabled = False
        them.Enabled = False
    End Sub
End Class